<template>
	<view class="content">
		<view class="reservation-form">
			<view class="reservation-form-li flex po before">
				<view class="reservation-form-li-left">
					头像     
				</view>
				
				<view class="reservation-form-li-right right">
					<image class="my-info-avatar" :src="url" mode="aspectFill" @tap="chooseAvatar"></image>
					<!-- <avatar 
						selWidth="600rpx" selHeight="600rpx" @upload="myUpload" :avatarSrc="url" :noTab="true"
						avatarStyle="width: 100upx; height: 100upx; margin: 0 30upx 0 0;border-radius: 100%;">
					</avatar> -->
				</view>
				
			</view>
			<view class="reservation-form-li flex po before">
				<view class="reservation-form-li-left">
					昵称     
				</view>
				<view class="reservation-form-li-right">
					<input placeholder-style="color:#c3c3c3" class="uni-input" name="input" v-model="data.nickname" placeholder="请输入昵称" />
				</view>
			</view>
			<view class="reservation-form-li flex po ">
				<view class="reservation-form-li-left">
					姓名     
				</view>
				<view class="reservation-form-li-right">
					<view class="disabled" >{{data.realname}} <text style="opacity: 0;">1</text> </view>
				</view>
			</view>
			<view class="reservation-form-li flex po before" @click="open('/pages/login/change-mobile/change-mobile')">
				<view class="reservation-form-li-left">
					手机号     
				</view>
				<view class="reservation-form-li-right">
					<view class="disabled" style="color: #000000;" >{{data.mobile}}</view>
				</view>
			</view>
			<view class="reservation-form-li flex po before" @click="open('/pages/mine/update-password/update-password')">
				<view class="reservation-form-li-left">
					系统密码     
				</view>
				<view class="reservation-form-li-right">
					<view class="disabled" style="color: #000000;opacity: 0;" >{{data.mobile}}</view>
				</view>
			</view>
			<view class="reservation-form-li flex po before" @click="open('/pages/mine/address-list/address-list')">
				<view class="reservation-form-li-left">
					地址管理     
				</view>
				<view class="reservation-form-li-right">
					<view class="disabled" style="color: #000000;opacity: 0;" >{{data.mobile}}</view>
				</view>
			</view>
			<view class="reservation-form-li flex po before">
				<view class="reservation-form-li-left">
					性别     
				</view>
				<view class="reservation-form-li-right">
					<input disabled @click="showSinglePicker" placeholder-style="color:#c3c3c3" v-model="pickerText" class="uni-input" name="input" placeholder="请选择性别" />
				</view>
			</view>	
			<!-- <view class="reservation-form-li flex po " v-if="data.inviter_info">
				<view class="reservation-form-li-left">
					邀请人     
				</view>
				<view class="reservation-form-li-right">
					<input placeholder-style="color:#c3c3c3" v-model="data.inviter_info.mobile" disabled class="uni-input" name="input" placeholder="请输入手机号" />
				</view>
			</view> -->
		</view>
		
		<!-- <view class="flex list-li po p1 before" @click="open('/pages/login/change-mobile/change-mobile')">
			<view class="">
				更换手机
			</view>
			<view class="">
				
			</view>
		</view> -->
		
		<view class="foot" @click="update">
			保存信息
		</view>
		<mpvue-picker :themeColor="themeColor" ref="mpvuePicker" :mode="mode" :deepLength="deepLength" :pickerValueDefault="pickerValueDefault"
		 @onConfirm="onConfirm" @onCancel="onCancel" :pickerValueArray="pickerValueArray"></mpvue-picker>
	</view>
</template>

<script>
	import {
        mapState,
        mapMutations
    } from 'vuex'
	import mpvuePicker from '../../../components/mpvue-picker/mpvuePicker.vue';
	import avatar from "../../../components/yq-avatar/yq-avatar.vue";
	
	
	// #ifdef H5
	
	// 压缩图片需要的一些元素和对象
	var reader = new FileReader(), img = new Image();
	
	// 缩放图片需要的canvas
	var canvas = document.createElement('canvas');
	var context = canvas.getContext('2d');
	// #endif
	
	
	export default {
		computed: {
            ...mapState(['token'])
        },
		components: {
			mpvuePicker,
            avatar
        },
		data() {
			return {
				url: "/static/logo_abf.png",
				h5Url: "static/logo_abf.png",
				pickerSingleArray: [{
						label: '男',
						value: 1
					},
					{
						label: '女',
						value: 2
					},
					{
						label: '保密',
						value: 0
					}
				],
				themeColor: '#007AFF',
				pickerText: '',
				mode: '',
				deepLength: 1,
				pickerValueDefault: [0],
				pickerValueArray:[],
				data: {},
				genderData:{
					0:"保密",
					1:"男",
					2:"女"
				},
				isUpdateLosding:false
			}
		},
		onShow() {
			this.getMemberInfo()
		},
		onLoad() {
			let _this = this
			// #ifdef H5
			// base64地址图片加载完毕后
			img.onload = function () {
			    // 图片原始尺寸
			    var originWidth = this.width;
			    var originHeight = this.height;
			    // 最大尺寸限制
			    var maxWidth = 400, maxHeight = 400;
			    // 目标尺寸
			    var targetWidth = originWidth, targetHeight = originHeight;
			    // 图片尺寸超过400x400的限制
			    if (originWidth > maxWidth || originHeight > maxHeight) {
			        if (originWidth / originHeight > maxWidth / maxHeight) {
			            // 更宽，按照宽度限定尺寸
			            targetWidth = maxWidth;
			            targetHeight = Math.round(maxWidth * (originHeight / originWidth));
			        } else {
			            targetHeight = maxHeight;
			            targetWidth = Math.round(maxHeight * (originWidth / originHeight));
			        }
			    }
			    // canvas对图片进行缩放
			    canvas.width = targetWidth;
			    canvas.height = targetHeight;
			    // 清除画布
			    context.clearRect(0, 0, targetWidth, targetHeight);
			    // 图片压缩
			    context.drawImage(img, 0, 0, targetWidth, targetHeight);
				
				_this.hUpload(canvas.toDataURL('image/png', '0.92'))
			};
			
			// 文件base64化，以便获知图片原始尺寸
			reader.onload = function(e) {
			    img.src = e.target.result;
			};
			// #endif
		},
		methods: {
			...mapMutations(['getNetworkType','vuexRequest','vuexUploadFile']),
			onCancel(e) {
				console.log(e)
			},
			myUpload(rsp) {
				//console.log(JSON.stringify(rsp))
				this.url = rsp.path; //更新头像方式一
				this.Upload(this.url)
				//rsp.avatar.imgSrc = rsp.path; //更新头像方式二
            },
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						console.log('chooseImage success, temp path is', res.tempFilePaths[0])
						var imageSrc = res.tempFilePaths[0]
						console.log(imageSrc)
						console.log(res.tempFiles[0])
						reader.readAsDataURL(res.tempFiles[0]);    
						
						return !1
						
					},
					fail: (err) => {
						console.log('chooseImage fail', err)
					}
				})
			},
			hUpload(path){
				this.isUpdateLosding=true	
				this.vuexUploadFile({
					url: '/api/v2/file/images?access-token=' + this.token,
					name: 'file',
					fileType: 'image',
					filePath: path,
					success: (res)  => {
						console.log(res)
						uni.hideLoading()
						this.data.head_portrait = res.data.data.url
						this.url = res.data.data.url
						console.log(this.data.head_portrait)
						this.h5Url = res.data.data.url
						uni.showToast({
							icon: 'none',
							title: res.data.message,
							position:'bottom'
						})
					},
					fail: (err)  => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: err.data.message,
							position:'bottom'
						})
					},
				})
			},
			Upload(path) {
				uni.showLoading({
					title: '上传中'
				});				
				this.vuexUploadFile({
					url: '/api/v2/file/images?access-token=' + this.token,
					name: 'file',
					fileType: 'image',
					filePath: path,
					success: (res)  => {
						uni.hideLoading()
						this.data.head_portrait = res.data.data.url
						uni.showToast({
							icon: 'none',
							title: res.data.message,
							position:'bottom'
						})
					},
					fail: (err)  => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: err.data.message,
							position:'bottom'
						})
					},
				})
			},
			showSinglePicker() {
				this.pickerValueArray = this.pickerSingleArray
				this.mode = 'selector'
				this.deepLength = 1
				this.pickerValueDefault = [0]
				this.$refs.mpvuePicker.show()
			},
			onConfirm(e) {
				this.pickerText = ''+e.label+''
				this.data.gender = ''+e.value+''
			},
			getMemberInfo() {
				this.vuexRequest({
					url: '/api/addons/execute?route=member/detail&addon=o-s-mall-dist&access-token=' + this.token,
					method: 'GET',
					data: {},
					success: (res)  => {
						this.data.inviter_info = res.data.data.inviter_info
						this.data.id = res.data.data.id
						this.data.username = res.data.data.username
						this.data.nickname = res.data.data.nickname
						this.data.realname = res.data.data.realname
						this.data.mobile = res.data.data.mobile
						this.data.gender = res.data.data.gender
						this.data.head_portrait = res.data.data.head_portrait
						this.pickerText = this.genderData[res.data.data.gender] 
						if(res.data.data.head_portrait){
							this.url = res.data.data.head_portrait
							this.h5Url = res.data.data.head_portrait
						}
					},
					fail: (err)  => {
					},
				})
			},
			update() {
				uni.showLoading({
					title:'正在上传',
					mask:true
				})
				this.vuexRequest({
					url: '/api/v2/member/member/'+this.data.id+'?access-token=' + this.token,
					method: 'PUT',
					data: this.data,
					success: (res)  => {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							position:'bottom',
							success: () => {
								uni.hideLoading()
							}
						})
						uni.navigateBack()
					},
					fail: (err)  => {
						uni.showToast({
							icon: 'none',
							title: err.data.message,
							position:'bottom'
						})				
					},
				})
			},
			open(url) {
				if(this.token){
					uni.navigateTo({
						url: url,
						fail(err) {
							console.log(err)
						}
					});
				}else{
					uni.navigateTo({
						url: '/pages/login/login/login'
					});
				}
			},
			chooseAvatar() {
				// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
				this.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/pages/uview-page/u-avatar-cropper',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 300,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 200,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
				/* uni.navigateTo({
					url:'/pages/uview-page/u-avatar-cropper',
					fail(err) {
						console.log(err)
					}
				}) */
			},
		},
		onBackPress() {
			if (this.$refs.mpvuePicker.showPicker) {
				this.$refs.mpvuePicker.pickerCancel();
				return true;
			}
			 
		},
		onUnload() {
			if (this.$refs.mpvuePicker.showPicker) {
				this.$refs.mpvuePicker.pickerCancel()
			}
			
		},
		created() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				this.url = path;
				console.log('===========img url',this.url)
				this.hUpload(this.url)
			})
		},
	}
</script>

<style>
	.p1{
		padding: 20rpx 30rpx; 
		background: #FFFFFF;
	}
	input:disabled{ 
		color: #CCCCCC;
	}
	.disabled{
		color: #CCCCCC;
		text-align: right;
		line-height: 80upx;
		padding-right: 20upx;
	}
	.mt1{
		margin: 20upx  0 0 0;
	}
	.flex{
		display: -webkit-box;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row;
		-ms-flex-direction: row;
		flex-direction: row;
		-webkit-box-pack: justify;
		-webkit-justify-content: space-between;
		-ms-flex-pack: justify;
		justify-content: space-between;
		-webkit-box-align: center;
		-webkit-align-items: center;
		-ms-flex-align: center;
		align-items: center;
	}
	.before:before{
		position: absolute;
		z-index: 10;
		right: 30upx;
		top: 50%;
		margin: -8upx 0 0 0;
		content: '';
		display: block;
		width: 16upx;
		height: 16upx;
		border: solid 2upx #d4d4d4;
		transform:rotate(45deg);
		border-left: none;
		border-bottom: none;
	}
	.po{
		position: relative;
	}
	.po:after{
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		-ms-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
	.uni-input{
		margin: 0;
		text-align: right;
	}
	.reservation-form-li{
		background: #FFFFFF;
		padding: 10upx 30upx;
	}
	.reservation-form-li-right{
		width: 75%;
		padding: 0 10upx 0 0 ; 
	}
	.store-ad{
		width: 750upx;
		height: 356upx;
	}
	.reservation-form-li-radio{
		padding: 30upx;
	}
	.foot{
		position: fixed;
		bottom: 0;
		color: #FFFFFF;
		width: 100%;
		height: 100upx;
		background: #e83e41;
		text-align: center;
		line-height: 100upx;
		z-index: 20;
	}
	.content{
		padding: 0 0 120upx 0;
	}
	.form-image{
		width: 100upx;
		height: 100upx;
		margin: 0 20upx 0 0;
	}
	.right{
		text-align: right;
		height: auto;
		padding: 0;
	}
	.my-info-avatar{
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		margin-right: 30rpx;
		box-shadow: 0 10rpx 30rpx -10rpx rgba(0,0,0,.6);
	}
</style>
